package com.hupun.service;

import java.util.Collection;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.hupun.mybatis.mapper.StandardMapper;
import com.hupun.mybatis.pojo.ProductStandard;
import com.hupun.mybatis.pojo.Products;

@Component
public class StandardService {
	@Resource
	private StandardMapper standardMapper;

	@DataProvider
	public Collection<ProductStandard> getStandard(String productCode) {
		return standardMapper.getStandard(productCode);
	}

	@DataResolver
	public void saveStandard(Collection<Products> productList) {
		for (Products product : productList) {
			Set<ProductStandard> productStandardSet = product
					.getProductStandardSet();
			if (productStandardSet != null && productStandardSet.size() > 0) {
				for (ProductStandard productStandard : productStandardSet) {
					if (EntityState.MODIFIED.equals(EntityUtils
							.getState(productStandard))) {
						
						standardMapper.updateStandard(productStandard);
					} else if (EntityState.NEW.equals(EntityUtils
							.getState(productStandard))) {
						
						standardMapper.insertStandard(productStandard,
								product.getProductCode());
					} else if (EntityState.DELETED.equals(EntityUtils
							.getState(productStandard))) {
						
						standardMapper.deletedStandard(productStandard);
					}
				}
			}
		}
	}
}